/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.impl;


/**
 * This class gives access to the current PersistenceManager associated with the current thread <br>
 * The PersistenceManager is stored as a "thread-local variable" ( see ThreadLocal for more details )
 * 
 * @author Laurent GUERIN
 * @since v 2.0
 *
 */
public final class CurrentPersistenceManager 
{
	
	//--- Thread-local variable
    private static final ThreadLocal<PersistenceManagerImpl> threadLocalVar = new ThreadLocal<PersistenceManagerImpl>();
    
//    //--------------------------------------------------------------------------------------
//    /**
//     * Returns the PersistenceManager instance associated with the current thread.<br>
//     * @return
//     */
//    public static final PersistenceManagerImpl get() 
//    {
//        //log.trace("get()");
//    	PersistenceManagerImpl pm = threadLocalVar.get();
//    	if ( pm != null )
//    	{
//    		return pm ;
//    	}
//    	else
//    	{
//    		throw new TelosysPersistenceException("No PersistenceManager for the current thread");
//    	}
//    }

    //--------------------------------------------------------------------------------------
    /**
     * Returns the PersistenceManager instance associated with the current thread.<br>
     * or null if there's no current PersistenceManager for the thread.
     * Method reserved for framework internal use.
     * @return 
     */
    protected static final PersistenceManagerImpl get() 
    {
        //log.trace("getNoException()");
    	return threadLocalVar.get();
    }
    
    //--------------------------------------------------------------------------------------
    /**
     * Sets the PersistenceManager instance associated with the current thread.
     * Method reserved for framework internal use.
     * @param pm
     */
    protected static final void set(PersistenceManagerImpl pm)
    {
    	threadLocalVar.set(pm);
    }

    //--------------------------------------------------------------------------------------
    /**
     * Removes the PersistenceManager instance associated with the current thread
     * if it's the same instance as the given PersistenceManager.
     * Method reserved for framework internal use.
     * @param pm
     * @return
     */
    protected static final boolean remove(PersistenceManagerImpl pm)
    {
    	PersistenceManagerImpl currentPersistenceManager = threadLocalVar.get();
    	if ( currentPersistenceManager != null )
    	{
    	    //--- Is it the current thread-local instance ?
            if ( currentPersistenceManager == pm )  
            {
                //--- Yes => Remove it !
            	threadLocalVar.set(null);
        		return true ; // removed
            }
    	}
    	return false ; // not removed
    }

    //--------------------------------------------------------------------------------------
    /**
     * Removes the DatabaseSession instance associated with the current thread.
     * Method reserved for framework internal use.
     */
    protected static final void remove() 
    {
        //log.trace("remove()");
    	threadLocalVar.set(null);
    }

}
